/*
 * Copyright (c) 2015, 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tchintech.pay.frame.presenter;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import java.util.ArrayList;

import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

import com.google.gson.Gson;
import com.tchintech.pay.MyApplication;
import com.tchintech.pay.frame.view.IDelegate;
import com.tchintech.pay.model.entity.Memberinfo;
import com.tchintech.pay.utils.JgPush;
import com.tchintech.pay.utils.SPUtils;


/**
 * Presenter base class for Fragment
 * Presenter层的实现基类
 *
 * @param <T> View delegate class type
 * @author kymjs (http://www.kymjs.com/) on 10/23/15.
 */
public abstract class FragmentPresenter<T extends IDelegate> extends Fragment {
    public T viewDelegate;
    protected Context context;
    public ProgressDialog pd;
    protected Gson gson;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            viewDelegate = getDelegateClass().newInstance();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        viewDelegate.create(inflater, container, savedInstanceState);
        ButterKnife.bind(this, viewDelegate.getRootView());
        return viewDelegate.getRootView();
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewDelegate.initWidget();

        gson = new Gson();
        initData();
        initView();
        bindEvenListener();
        setJg();
    }

    /**
     * 初始化极光推送
     */
    private void setJg() {
        //判断极光是否注册成功
        String rid = JPushInterface.getRegistrationID(getActivity().getApplicationContext());
        System.out.println("okhttp极光" + rid);
        if (rid.equals("") || rid == null) {
            // 初始化 JPush
            JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
            JPushInterface.init(getActivity());            // 初始化 JPush
            System.out.println("okhttp初始化极光");
        } else {
            //如果极光id存在的话也去做一次修改
            //规避2个设备上来会登陆导致极光id不能实时修改
            JgPush jgPush = new JgPush(getActivity(), rid);
        }
    }

    protected void initView() {

    }

    protected Gson getGson() {
        return gson;
    }

    protected void initData() {

    }

    protected void bindEvenListener() {
    }

    public void showMsg(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    public void showProgressDialog(String title, String text) {
        pd = ProgressDialog.show(getActivity(), title, text);
    }

    public void hideProgressDialog() {
        if (pd != null) {
            pd.dismiss();
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        if (viewDelegate.getOptionsMenuId() != 0) {
            inflater.inflate(viewDelegate.getOptionsMenuId(), menu);
        }
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (viewDelegate == null) {
            try {
                viewDelegate = getDelegateClass().newInstance();
            } catch (java.lang.InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewDelegate = null;
    }

    protected abstract Class<T> getDelegateClass();

    public Memberinfo getMemberinfo() {
        return ((MyApplication) getActivity().getApplication()).getMemberinfo();
    }
}
